<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>checkregister</title>
    <link rel="stylesheet" href="/static/lib/css/layui.css" media="all">
</head>

<body>

    <blockquote class="layui-elem-quote layui-text">
        注意: 必须首先指定某个班级 每次签到以创建时间区分
    </blockquote>

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
        <legend>查看签到情况</legend>
    </fieldset>

    <!-- <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script> -->

    <form class="layui-form layui-form-pane" action="" lay-filter="">

        <div class="layui-form-item">
            <label class="layui-form-label">选择班级</label>
            <div class="layui-input-inline">
                <select name="classname" lay-verify="required" lay-search="" lay-filter="chooseclass" id='chooseclass'>
                    <option value="">若不存在班级请先创建</option>
                    {% for class in classes %}
                    <option value="{{ class.id }}">{{ class.classname }}</option>
                    {% endfor %}
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <!-- <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="form2">添加到班级</button>
            </div> -->
            <label class="layui-form-label">修改签到</label>
            <div class="layui-btn-group demoTable">
                <button type="button" class="layui-btn" data-type="" lay-submit="" lay-filter="submit1">设为出勤</button>
                <button type="button" class="layui-btn" data-type="" lay-submit="" lay-filter="submit2">设为请假</button>
                <button type="button" class="layui-btn" data-type="" lay-submit="" lay-filter="submit3">设为旷课</button>
            </div>
            <div class="demoTable" style="margin-top: 15px;">
                学生ID：
                <div class="layui-inline">
                    <input type="text" class="layui-input" id="search_sid" autocomplete="off">
                </div>
                <button type="button" class="layui-btn" data-type="reload">搜索</button>
                <table class="layui-hide" id="LAY_table_user" lay-filter="user"></table>
            </div>

            
        </div>

    </form>

    <script src="/static/lib/layui.js" charset="utf-8"></script>
    <script src="/static/lib/axios.min.js"></script>
    <script>
        layui.use(['form', 'layedit', 'laydate'], function () {
            var form = layui.form
                , layer = layui.layer
                , layedit = layui.layedit
                , laydate = layui.laydate
                , $ = layui.$
                , table = layui.table;

            var $ = layui.$, active = {
                reload: function () {
                    var search_sid = $('#search_sid'); // 获取搜索内容
                    var chooseclass = $('#chooseclass')
                    //执行重载
                    table.reload('reload_table', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        , where: {
                            classid: chooseclass.val(),
                            student_id: search_sid.val()
                        }
                        , url: '../api/register'
                    });
                }
            };

            $('.demoTable .layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
            //监听表格复选框选择
            /* table.on('checkbox()', function (obj) {
                console.log(obj)
            }); */

            form.on('select(chooseclass)', function (data) {
                console.log(data.value); //得到被选中的值
                classid = data.value
                table.render({
                    elem: '#LAY_table_user'
                    , url: '../api/register?classid='+classid
                    , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                    , defaultToolbar: ['filter', 'exports', 'print']
                    , cols: [[
                        { checkbox: true, fixed: true }
                        , { field: 'id', title: 'ID', width: 80, sort: true, fixed: 'left' }
                        , { field: 'student_id', title: '学生id', width: 95, sort: true }
                        , { field: 'class_id', title: '班级id', width: 95, sort: true }
                        , { field: 'create_time', title: '发起时间', sort: true, width: 160 }
                        , { field: 'time', title: '签到时间', sort: true, width: 160 }
                        , { field: 'result', title: '结果', width: 90, fixed: 'right', sort: true }
                    ]]
                    , page: true
                    , id: 'reload_table'
                    , height: 'ful-400'
                });
            });

            form.on('submit(submit1)', function (data) {
                console.log("设为出勤")
                setResult("出勤")
                return false;
            });

            form.on('submit(submit2)', function (data) {
                console.log("设为请假")
                setResult("请假")
                return false;
            });

            form.on('submit(submit3)', function (data) {
                console.log("设为旷课")
                setResult("旷课")
                return false;
            });

            function setResult(result) {
                var checkStatus = table.checkStatus('reload_table')
                    , tdata = checkStatus.data;
                let idlist = []
                for (let s of tdata) {
                    //console.log(s.id)
                    idlist.push(s.id)
                }
                idlist = idlist.join('-')
                console.log(idlist)
                axios({
                    method: 'put',
                    url: '../api/register',
                    data: "result=" + result + "&registerids=" + idlist,
                })
                    .then(function (response) {
                        console.log(response.data);
                        result = response.data.result
                        if (result == "no-exist-register") {
                            sid = response.data.id
                            layer.msg("该register id不存在: " + sid)
                        } else if (result == "nothing-change") {
                            layer.msg("未改变任何签到结果")
                        } else {
                            layer.msg("成功")
                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            }

        });
    </script>

</body>

</html>